using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using T2.WebSecurity.ASPMVC.Entity.Interface;
using T2.WebSecurity.ASPMVC.Entity;
using T2.WebSecurity.Business.Entities;
using T2.WebSecurity.Business.Entities.Interfaces;
using T2.WebSecurity.ASPMVC.Entity.Search;
using T2.WebSecurity.ASPMVC.Entity.Search.Interface;
using T2.WebSecurity.Business.Entities.Search;
using T2.WebSecurity.Business.Entities.Search.Interfaces;
using T2.WebSecurity.ASPMVC.Entity.Translators;
using T2.WebSecurity.ASPMVC.Entity.Translators.Search;
using System.Web.Security;
using System.Activities;
using T2.WebSecurity.Business.Workflows;


namespace T2.WebSecurity.ASPMVC.ViewModel
{

	public class UsersInRolesViewModel : BaseViewModel
	{
		Business.BusinessService bs = new Business.BusinessService();
		public UsersInRolesViewModel(bool loadParent)
		{
		if (loadParent)
			{
				this.LoadUserProfileList();
				this.LoadApplicationRolesList();
			}
		}
		
		private String m_header;
		public String Header
        {
            get
            {
                return "Users In Roles";
            }
			
			set
            {
                m_header = value;
            }
        }
		
		private String m_title;
		public String Title
        {
            get
            {
                return "Users In Roles";
            }
			
			set
            {
                m_title = value;
            }
        }
		
		private IUsersInRoles_ui m_model;
		public IUsersInRoles_ui Model
		{
			get
			{
				return m_model;
			}
			set
			{
				m_model = value;
			}
		}
		private List<IUserProfile_ui> m_userprofilelist;
		public List<IUserProfile_ui> UserProfile_List
		{
			get
			{
				return m_userprofilelist;
			}
			set
			{
				m_userprofilelist = value;
			}
		}

		private List<IApplicationRoles_ui> m_applicationroleslist;
		public List<IApplicationRoles_ui> ApplicationRoles_List
		{
			get
			{
				return m_applicationroleslist;
			}
			set
			{
				m_applicationroleslist = value;
			}
		}

        



		 private void LoadUserProfileList()
		{
			IUserProfile_biz bizParam = new UserProfile_biz();
			bizParam.Action = "GetAll";
			bizParam.PageNumber = 1;
			bizParam.PageSize = 20;
			bizParam.SortColumn = "UserId";
			bizParam.SortType = "desc";
			bizParam.TotalRows = -1;

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", bizParam);
            wfInput.Add("ParamSearch", null);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserId", (int)User.ProviderUserKey);
            wfInput.Add("UserName", User.UserName);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new UserProfileQuery(), wfInput);

            IUserProfile_biz[] retValBiz = (IUserProfile_biz[])wfOutput["RetValCollection"];

            //List<IUserProfile_biz> retVal = FilterListByLoginUser(bs.UserProfileController_Get(bizParam, true, true));
			List<IUserProfile_ui> LoadRetVal = new List<IUserProfile_ui>();
			UserProfileUITranslator trans = new UserProfileUITranslator();
            foreach (IUserProfile_biz item in retValBiz)
			{
				IUserProfile_ui temp = new UserProfile_ui();
				temp = trans.TranslateFromBusinessLayer(item);
				LoadRetVal.Add(temp);
			}
			m_userprofilelist = LoadRetVal;
		}

		 private void LoadApplicationRolesList()
		{
			IApplicationRoles_biz bizParam = new ApplicationRoles_biz();
            bizParam.Action = "GetAll";
            bizParam.PageNumber = 1;
            bizParam.PageSize = 20;
            bizParam.SortColumn = "ApplicationRoleId";
            bizParam.SortType = "desc";
            bizParam.TotalRows = -1;

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", bizParam);
            wfInput.Add("ParamSearch", null);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserId", (int)User.ProviderUserKey);
            wfInput.Add("UserName", User.UserName);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new ApplicationRolesQuery(), wfInput);

            IApplicationRoles_biz[] retValBiz = (IApplicationRoles_biz[])wfOutput["RetValCollection"];

            //List<IApplicationRoles_biz> retValBiz = FilterListByLoginUser(bs.ApplicationRolesController_Get(param, true, true));
			List<IApplicationRoles_ui> LoadRetVal = new List<IApplicationRoles_ui>();
			ApplicationRolesUITranslator trans = new ApplicationRolesUITranslator();
            foreach (IApplicationRoles_biz item in retValBiz)
			{
				IApplicationRoles_ui temp = new ApplicationRoles_ui();
				temp = trans.TranslateFromBusinessLayer(item);
				LoadRetVal.Add(temp);
			}
			m_applicationroleslist = LoadRetVal;
		}


        
		
		public void LoadModel(IUsersInRoles_ui param)
        {
            UsersInRolesUITranslator trans = new UsersInRolesUITranslator();
            IUsersInRoles_biz bizParam = trans.TranslateToBusinessLayer(param);

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", bizParam);
            wfInput.Add("ParamSearch", null);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserId", (int)User.ProviderUserKey);
            wfInput.Add("UserName", User.UserName);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new UsersInRolesQuery(), wfInput);

            IUsersInRoles_biz retValBiz = (IUsersInRoles_biz)wfOutput["RetVal"];

            //List<IUsersInRoles_biz> tempRetVal = FilterListByLoginUser(bs.UsersInRolesController_Get(bizParam, true, true));
            IUsersInRoles_ui retVal = new UsersInRoles_ui();
            if (retValBiz != null)
            {
                retVal = trans.TranslateFromBusinessLayer(retValBiz);
            }

            m_model = retVal;

        }

        public Int32 Set(IUsersInRoles_ui param)
        {
            UsersInRolesUITranslator trans = new UsersInRolesUITranslator();
            IUsersInRoles_biz bizParam = trans.TranslateToBusinessLayer(param);

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", bizParam);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserId", (int)User.ProviderUserKey);
            

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new UsersInRolesNonQuery(), wfInput);
            IDictionary<string, int> r = (IDictionary<string, int>)wfOutput["RetVal"];

            //IDictionary<string,int> r = bs.UsersInRolesController_Set(bizParam);
            var ret = r.ToList().Select(s => new { Key = s.Key, Value = s.Value });
            Int32 retVal = ret.Where(z => z.Key == "Return_Value").Select(m => m.Value).FirstOrDefault();
			return retVal;
        }

       

        public List<IUsersInRoles_ui> SearchList(IUsersInRolesSearch_ui param)
        {
            UsersInRolesUISearchTranslator transSearch = new UsersInRolesUISearchTranslator();
            IUsersInRolesSearch_biz bizParam = transSearch.TranslateToBusinessLayer(param);

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", null);
            wfInput.Add("ParamSearch", bizParam);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserId", (int)User.ProviderUserKey);
            wfInput.Add("UserName", User.UserName);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new UsersInRolesQuery(), wfInput);

            IUsersInRoles_biz[] retValBiz = (IUsersInRoles_biz[])wfOutput["RetValCollection"];

            //List<IUsersInRoles_biz> retValBiz = FilterListByLoginUser(bs.UsersInRolesController_Search(bizParam, true, true));
            List<IUsersInRoles_ui> retVal = new List<IUsersInRoles_ui>();
            UsersInRolesUITranslator trans = new UsersInRolesUITranslator();
            foreach (IUsersInRoles_biz item in retValBiz)
            {
                IUsersInRoles_ui temp = trans.TranslateFromBusinessLayer(item);
                retVal.Add(temp);
            }

            
            return retVal;

        }

        

        public List<IUsersInRoles_ui> ModelList(IUsersInRoles_ui param)
        {

            UsersInRolesUITranslator trans = new UsersInRolesUITranslator();
            IUsersInRoles_biz bizParam = trans.TranslateToBusinessLayer(param);


            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", bizParam);
            wfInput.Add("ParamSearch", null);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserId", (int)User.ProviderUserKey);
            wfInput.Add("UserName", User.UserName);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new UsersInRolesQuery(), wfInput);

            IUsersInRoles_biz[] retValBiz = (IUsersInRoles_biz[])wfOutput["RetValCollection"];

            //List<IUsersInRoles_biz> retValBiz = FilterListByLoginUser(bs.UsersInRolesController_Get(bizParam, true, true));
            List<IUsersInRoles_ui> retVal = new List<IUsersInRoles_ui>();
            foreach (IUsersInRoles_biz item in retValBiz)
            {
                IUsersInRoles_ui temp = trans.TranslateFromBusinessLayer(item);
                retVal.Add(temp);
            }

            
            return retVal;

        }

        
		
	}
}
